# -*-coding:utf-8-*-
import sqlite3

import requests
import json
import time

from wadeTools.wadeTools_email import sendEmail

import captcha
import base64
from lxml import etree
import encry_util
import sys
sys.path.append("..")
from wadeTools.wadeTools_Print import wadeRedPrint
'''
lofter爬虫

生成相关库 pip freeze > requirements.txt
安装相关库 pip install -r requirements.txt

'''
send_topic_file_path = "发帖账号.txt"
# phone = "采集账号"
# password = "采集密码"
phone = "17734750612"
password = "Wfzh@2023"
#数据库地址
db_host = ""
#数据库端口
db_port = "5432"
#数据库名称
db_name = ""
#数据库用户名
db_user = ""
#数据库密码
db_password = ""



def login():
    param = {
        "phone": phone,
        "password": password,
    }
    r = requests.post("https://upup.lofter.com/api/kol/login/doLogin", data=param)
    r.encoding = 'utf-8'
    json_obj = json.loads(r.text)
    wadeRedPrint(json_obj)
    return json_obj['data']['token']


def fetch_data(user_token):
    offset = 0
    limit = 20
    totalCount = 0
    headers = {
        "Cookie": "_kol_token=" + user_token,
    }
    mySql = get_db_connection()
    while True:
        # print("采集第" + str(offset / limit + 1) + "页数据")
        wadeRedPrint("采集第" + str(offset / limit + 1) + "页数据")
        param = {
            "limit": limit,
            "offset": offset,
            "settlementType": "CPS",
            "type": "post",
        }
        json_obj = requests.get("https://upup.lofter.com/api/kol/content/query", params=param, headers=headers).json()
        totalCount = json_obj['data']['total']
        wadeRedPrint(json_obj)

        for data in json_obj['data']['list']:
            resourceId = data['resourceId']
            resourceUrl = data['resourceUrl']
            title = data['post']['title']
            wadeRedPrint(title)
            # 获取当前日期
            current_date = time.strftime("%Y-%m-%d", time.localtime())
            # 查询数据库获取ResourceId是否已经存在
            result = mySql.checkExist(keyName="resource_id",value=resourceId)
            wadeRedPrint(result)

            if result==True:
                print("资源已经存在", title)
                continue
            else:
                wadeRedPrint("新资源，插入")
            publicize_url = get_publicize_url(user_token, resourceId)
            content,tags = get_resource_body(resourceUrl)
            # wadeRedPrint(content)
            try:
                mySql.insertdb((resourceId, resourceUrl, title, content.replace("'", "\\'").replace("'", "''"), tags,
                                current_date, publicize_url, phone, '0', '', ''))
                print('插入成功', title)
            except sqlite3.OperationalError:
                wadeRedPrint("插入失败，跳过")
                continue
            time.sleep(1)
        # 页码增加
        if offset >= totalCount:
            break
        else:
            offset += limit
        time.sleep(5)

    print("采集完成")


def get_resource_body(url):
    r = requests.get(url)
    html_ele = etree.HTML(r.text)
    nodes = html_ele.xpath('//div[@class="text"]/p')
    body = ''
    for node in nodes:
        # 过滤空白内容
        # if node.text == None:
        #     continue
        #print(node.itertext)
        # 使用tostring函数获取HTML表示形式
        html_representation = etree.tostring(node, method="html", encoding="utf-8").decode("utf-8")
        # 打印或使用html_representation变量
        #print(html_representation)
        # body += node.text + "\n"
        body += html_representation
    tags = []
    nodes = html_ele.xpath('//div[@class="tag"]/a')
    for node in nodes:
        tags.append(node.text.replace("● ",""))
    return body,",".join(tags)


def get_db_connection():
    # 新建数据库
    from wadeTools import wadeTools_db_Sqlite

    mySql = wadeTools_db_Sqlite.wadeDb(dbFilePath="data.db", tableName='tb_lofter', typeList=(
        {"字段名": "resource_id", "字段类型": "TEXT unique"},
        {"字段名": "resource_url", "字段类型": "TEXT"},
        {"字段名": "resource_title", "字段类型": "TEXT"},
        {"字段名": "resource_body", "字段类型": "TEXT"},
        {"字段名": "resource_tag", "字段类型": "TEXT"},
        {"字段名": "create_time", "字段类型": "TEXT"},
        {"字段名": "publicize_url", "字段类型": "TEXT"},
        {"字段名": "publicize_user", "字段类型": "TEXT"},
        {"字段名": "is_send", "字段类型": "TEXT"},
        {"字段名": "send_user", "字段类型": "TEXT NULL"},
        {"字段名": "send_time", "字段类型": "TEXT NULL"},
    ))
    mySql.initdb()
    # conn = psycopg2.connect(database=db_name, user=db_user, password=db_password, host=db_host, port=db_port)
    return mySql


# 获取推广链接
def get_publicize_url(user_token, resource_id):
    param = {
        "resourceId": resource_id,
        "resourceType": "post",
    }
    headers = {
        "Cookie": "_kol_token=" + user_token,
    }
    r = requests.post("https://upup.lofter.com/api/kol/proword/newLink", data=param, headers=headers).json()
    return r['data']['url']


#############################################


class Lofter:
    AES_KEY = 'a2sklltoa538q174'
    AES_IV = '5037owpm4251rpst'
    # 登录重试次数
    login_retry_count = 0
    max_topic_day = 4

    def __init__(self, user, pass_word):
        self.user = user
        self.pass_word = pass_word
        self.cookie = ''
        self.capttoken = ''
        self.enc = encry_util.Encrypt(key=self.AES_KEY, iv=self.AES_IV)
        self.userid = ''
        self.blogName = ''

    def login(self):
        if self.login_retry_count > 2:
            return False
        param = {
            "phone": self.user,
            "passport": self.enc.sha256(self.pass_word),
            "type": 0,
            "clientType": 0,
            "deviceType": 3,
            "Target": "www.lofter.com"
        }
        headers = {
            "Cookie": self.cookie,
            "capttoken": self.capttoken,
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
        }
        url = "https://www.lofter.com/lpt/login.do?product=lofter-pc&_=" + str(int(time.time() * 1000))
        r = requests.post(url, data=param, headers=headers)
        for name, value in r.cookies.items():
            self.cookie += '{0}={1};'.format(name, value)
        if "滑动滑块" in r.text:
            self.login_retry_count += 1
            self.get_captcha()
            return self.login()
        elif "token" in r.text:
            json_obj = r.json()
            self.userid = json_obj['userid']
            self.token = json_obj['token']
            self.cookie = self.cookie + "LOFTER-PHONE-LOGIN-AUTH=" + self.token + ";"
            print(self.user + " 登录成功")
            return True
        else:
            return False

    # 获取滑块识别返回Token
    def get_captcha(self):
        headers = {
            "Content-Type": "application/json",
            "Referer": "https://www.lofter.com/front/login",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
            "Cookie": self.cookie,
        }
        print('获取滑块中...')
        json_obj = requests.get("https://captcha.lofter.com/captcha/get?type=jigsaw", headers=headers).json()
        # wadeRedPrint(json_obj)

        id = json_obj['data']['id']
        bg = json_obj['data']['bg']

        front= json_obj['data']['front']

        x_pos = captcha.calc_captcha(bg, front)
        # {"id":"b839f434492d459ca3e7364b9951edbe","type":"jigsaw","data":"{\\"xPos\\":80}"}
        data = {
            "id": id,
            "type": "jigsaw",
            "data": json.dumps({"xPos": x_pos})
        }
        data = base64.b64decode(self.enc.aes_encrypt(json.dumps(data)))
        headers['x-encseckey'] = self.enc.rsa_encrypt(self.AES_KEY + "-" + self.AES_IV)
        raw = requests.post("https://captcha.lofter.com/captcha/check", data=data, headers=headers)
        result = self.enc.aes_decrypt(base64.b64encode(raw.content)).replace('\x04', '')
        # print(get_middle_text(result,'"token":"','"'))
        json_obj = json.loads(result)
        self.capttoken = json_obj['data']['token']
        return True
    def get_blog_info(self):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
            "Cookie": self.cookie,
        }
        r = requests.get("https://www.lofter.com/newweb/home/getBlogInfo.json", headers=headers)
        self.blogName = r.json()['data']['blogInfo']['blogName']
        # print(r.text)
    # 发帖
    def send_topic(self, title, content, tag):
        param = {
            "blogId": self.userid,
            "blogName": self.blogName,
            "content": content,
            "allowView": 0,
            "isPublished": True,
            "cctype": 0,
            "tag": tag,
            "collectionId": 0,
            "syncSites": "",
            "forbidShare": 0,
            "allowReward": 0,
            "plans": [],
            "title": title,
            "photoInfo": [],
            "payView": 0,
            "freeWords": 0,
            "valCode": ""
        }
        headers = {
            "Cookie": self.cookie,
            "capttoken": self.capttoken,
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
            "Referer":"https://www.lofter.com/",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
        }
        url = "https://www.lofter.com/blog/"+ self.blogName +"/new/text/"
        r = requests.post(url, data=param, headers=headers)
        print(r.text)
        if "滑动滑块" in r.text:
            wadeRedPrint("遇到滑块")
            self.get_captcha()
            return self.send_topic(title, content, tag)
        if "id" in r.text:
            wadeRedPrint(url)
            wadeRedPrint(r.text)
            return True
        return False

def fetch_topic_user():
    # 打开文件
    with open(send_topic_file_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        for line in lines:
            line = line.strip()
            if line == '':
                continue
            args = line.split('----')
            lofter = Lofter(args[0], args[1])
            # 检测当天已发送数
            mySql = get_db_connection()
            current_date = time.strftime("%Y-%m-%d", time.localtime())
            # 查询 符合某个值内容的所有行
            # results = mySql.searchAll_byKeyName(keyName='send_user',value=lofter.user)
            results = mySql.searchAll_byTwoKeyName(keyName1='send_time',value1=f"'{current_date}'",keyName2='send_user',value2= lofter.user)
            # 已发数
            issued = len(results)
            wadeRedPrint(issued)
            wadeRedPrint(results,issued,current_date,lofter.user)
            # exit()
            if issued > lofter.max_topic_day:
                print("超过当天发帖数，放弃。")
                continue
            loginSuccess = lofter.login()
            # 登录
            if loginSuccess is False:
                print(lofter.user + " 登录失败")
                continue
            lofter.get_blog_info()
            rows = mySql.searchAll_byKeyName(keyName="is_send",value='0')
            # cursor.execute("SELECT id,resource_title,resource_body,resource_tag,publicize_url FROM tb_lofter where is_send = '0'")

            for row in rows:
                id = row[0]
                topic_title = "【推荐】" + row[2]
                topic_content = row[3].replace('\\xa0', '').replace('\\u3000','\u3000').replace('\\u2003','').replace('\\r','').replace('\\n','')
                topic_tag = row[4]
                topic_url = row[6]
                topic_content += '</br><p id="p_uallqvqfbec">&nbsp&nbsp&nbsp<strong>    <a href="'+ topic_url +'" target="_blank">🛑点击这里查看后续🛑</a></strong></p><p></p><p></p>&nbsp&nbsp&nbsp(点那个红点直接跳转)'
                isOk = lofter.send_topic(topic_title,topic_content,topic_tag)
                if isOk:
                    issued+=1
                    print(topic_title + '发布成功！')
                    # 更新
                    now = time.strftime("%Y-%m-%d", time.localtime())
                    mySql.updateValue(byKey="resource_id",byKey_value=id,changeKey="is_send",changeKey_value=1)
                    mySql.updateValue(byKey="resource_id",byKey_value=id,changeKey="send_time",changeKey_value=now)
                    mySql.updateValue(byKey="resource_id",byKey_value=id,changeKey="send_user",changeKey_value=lofter.user)
                else:
                    print("发布失败！")      
                if  issued > lofter.max_topic_day:
                    print("超过当天发帖数，放弃。")
                    break
                time.sleep(10)

            # 休眠
            time.sleep(10)
        # notify.send("老福特","当天工作已经全部完成")


def get_middle_text(text, front_text, end_text):
    front_pos = text.find(front_text)
    end_pos = text.find(end_text, front_pos + len(front_text))
    return text[front_pos + len(front_text):end_pos]


def main():
    if False:
        # 采集
        user_token = login()
        wadeRedPrint(user_token)
        fetch_data(user_token)
    else:
        # 发帖
        fetch_topic_user()
        now = time.strftime("%Y-%m-%d", time.localtime())
        sendEmail('317909531@qq.com', f'{now}', '所有账号发布成功')
        print(f"{now}完成一次")


if __name__ == "__main__":
    # mySql = get_db_connection()
    # result =mySql.searchAll_byKeyName(keyName='resource_id',value='11691024617')
    # print(result)
    # print(result[0][3].replace('\\xa0', '').replace('\\u3000','\u3000').replace('\\u2003','').replace('\\r','').replace('\\n',''))
    main()
    # 完成后重置数据库
    conn = sqlite3.connect('data.db')
    cursor = conn.cursor()
    cursor.execute("UPDATE tb_lofter SET send_user = '',is_send=0,send_time=''")
    conn.commit()
    cursor.close()
    conn.close()
